

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>pyamp.web.http.requests &mdash; pyamp v1.2 documentation</title>
    <link rel="stylesheet" href="../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../../',
        VERSION:     '1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <link rel="top" title="pyamp v1.2 documentation" href="../../../../index.html" />
    <link rel="up" title="Module code" href="../../../index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../../index.html" accesskey="U">Module code</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for pyamp.web.http.requests</h1><div class="highlight"><pre>
<span class="c"># Copyright 2012 Brett Ponsler</span>
<span class="c"># This file is part of pyamp.</span>
<span class="c">#</span>
<span class="c"># pyamp is free software: you can redistribute it and/or modify</span>
<span class="c"># it under the terms of the GNU General Public License as published by</span>
<span class="c"># the Free Software Foundation, either version 3 of the License, or</span>
<span class="c"># (at your option) any later version.</span>
<span class="c">#</span>
<span class="c"># pyamp is distributed in the hope that it will be useful,</span>
<span class="c"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="c"># GNU General Public License for more details.</span>
<span class="c">#</span>
<span class="c"># You should have received a copy of the GNU General Public License</span>
<span class="c"># along with pyamp.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
<span class="sd">&#39;&#39;&#39;The requests module contains classes pertaining to creating</span>
<span class="sd">various types of objects to handle HTTP requests.</span>

<span class="sd">&#39;&#39;&#39;</span>
<span class="kn">from</span> <span class="nn">urllib</span> <span class="kn">import</span> <span class="n">unquote</span>
<span class="kn">from</span> <span class="nn">BaseHTTPServer</span> <span class="kn">import</span> <span class="n">BaseHTTPRequestHandler</span>

<span class="kn">from</span> <span class="nn">pyamp.util</span> <span class="kn">import</span> <span class="n">getStackTrace</span>
<span class="kn">from</span> <span class="nn">pyamp.web.http.constants</span> <span class="kn">import</span> <span class="n">StatusCodes</span>


<div class="viewcode-block" id="BaseRequestHandler"><a class="viewcode-back" href="../../../../web.html#pyamp.web.http.requests.BaseRequestHandler">[docs]</a><span class="k">class</span> <span class="nc">BaseRequestHandler</span><span class="p">(</span><span class="n">BaseHTTPRequestHandler</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;The BaseRequestHandler class provides the basic functionality that</span>
<span class="sd">    is shared among all HTTP request handlers.</span>

<span class="sd">    &#39;&#39;&#39;</span>
    <span class="n">pages</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="sd">&#39;&#39;&#39;The pages property contains the dictionary of pages that this</span>
<span class="sd">    request handler is able to retrieve.</span>

<span class="sd">    &#39;&#39;&#39;</span>

    <span class="c"># @todo: add default error pages</span>
    <span class="n">errorPages</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="sd">&#39;&#39;&#39;The errorPages property contains the dictionary mapping error codes</span>
<span class="sd">    to the pages to display for those particular errors.</span>

<span class="sd">    &#39;&#39;&#39;</span>

<div class="viewcode-block" id="BaseRequestHandler.do_GET"><a class="viewcode-back" href="../../../../web.html#pyamp.web.http.requests.BaseRequestHandler.do_GET">[docs]</a>    <span class="k">def</span> <span class="nf">do_GET</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Process a GET request.&#39;&#39;&#39;</span>
        <span class="n">errorCode</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="n">content</span> <span class="o">=</span> <span class="bp">None</span>

        <span class="c"># Get the path and the arguments for this request</span>
        <span class="n">path</span><span class="p">,</span> <span class="n">arguments</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__getPathAndArguments</span><span class="p">()</span>

        <span class="c"># Attemp to display the requested page</span>
        <span class="n">page</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__findPage</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">page</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="c"># Request the page with the path, and arguments</span>
                <span class="c">#page.get(self.headers, arguments)</span>
                <span class="n">content</span> <span class="o">=</span> <span class="n">page</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">arguments</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">Exception</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">log_message</span><span class="p">(</span><span class="n">getStackTrace</span><span class="p">(),</span> <span class="n">e</span><span class="p">)</span>
                <span class="n">errorCode</span> <span class="o">=</span> <span class="n">StatusCodes</span><span class="o">.</span><span class="n">InternalServerError</span>

        <span class="k">if</span> <span class="n">errorCode</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">content</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">errorCode</span> <span class="o">=</span> <span class="n">StatusCodes</span><span class="o">.</span><span class="n">FileNotFound</span>

        <span class="c"># Display an error page if an error was encountered</span>
        <span class="k">if</span> <span class="n">errorCode</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">page</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__findErrorPage</span><span class="p">(</span><span class="n">errorCode</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">page</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
                <span class="n">content</span> <span class="o">=</span> <span class="n">page</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">arguments</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">log_message</span><span class="p">(</span><span class="s">&quot;Unable to locate error page for [</span><span class="si">%s</span><span class="s">]&quot;</span> <span class="o">%</span> \
                                     <span class="n">errorCode</span><span class="p">)</span>

        <span class="c"># @todo: if page is None here OR it is not initialized, then use a</span>
        <span class="c"># backup Not-found page</span>
        <span class="k">if</span> <span class="n">page</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">content</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__sendResponse</span><span class="p">(</span><span class="n">page</span><span class="p">,</span> <span class="n">content</span><span class="p">,</span> <span class="n">errorCode</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="BaseRequestHandler.do_POST"><a class="viewcode-back" href="../../../../web.html#pyamp.web.http.requests.BaseRequestHandler.do_POST">[docs]</a>    <span class="k">def</span> <span class="nf">do_POST</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Process a POST request.&#39;&#39;&#39;</span>
        <span class="c"># @todo: complete this function</span>
        <span class="k">pass</span>
</div>
<div class="viewcode-block" id="BaseRequestHandler.log_message"><a class="viewcode-back" href="../../../../web.html#pyamp.web.http.requests.BaseRequestHandler.log_message">[docs]</a>    <span class="k">def</span> <span class="nf">log_message</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">format</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Log a message.</span>

<span class="sd">        * format -- The message format</span>
<span class="sd">        * args -- The message arguments</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="c"># @todo: use a logger, or log level here</span>
        <span class="n">BaseHTTPRequestHandler</span><span class="o">.</span><span class="n">log_message</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">format</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">)</span>
</div>
    <span class="k">def</span> <span class="nf">__getPathAndArguments</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Get the actual path name, and a dictionary of the arguments</span>
<span class="sd">        passed in the path.</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="c"># Unquote the path to get rid of http encoding</span>
        <span class="n">path</span> <span class="o">=</span> <span class="n">unquote</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">path</span><span class="p">)</span>
        <span class="n">arguments</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="c"># Find the start of the URL arguments</span>
        <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s">&quot;?&quot;</span><span class="p">)</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
            <span class="n">path</span><span class="p">,</span> <span class="n">strArguments</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;?&quot;</span><span class="p">)</span>

            <span class="c"># Add each argument to the argument dictionary</span>
            <span class="k">for</span> <span class="n">argument</span> <span class="ow">in</span> <span class="n">strArguments</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;&amp;&quot;</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">argument</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s">&quot;=&quot;</span><span class="p">)</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
                    <span class="n">name</span><span class="p">,</span> <span class="n">value</span> <span class="o">=</span> <span class="n">argument</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;=&quot;</span><span class="p">)</span>

                    <span class="c"># Note: Convert all argument names to lowercase</span>
                    <span class="n">arguments</span><span class="p">[</span><span class="n">name</span><span class="o">.</span><span class="n">lower</span><span class="p">()]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__cleanHtml</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">path</span><span class="p">,</span> <span class="n">arguments</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">__cleanHtml</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Clean the given HTML string.</span>

<span class="sd">        * value -- The HTML string</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="c"># All the strings with their respective replacements</span>
        <span class="n">replacements</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s">&quot;+&quot;</span><span class="p">:</span> <span class="s">&quot; &quot;</span>
            <span class="p">}</span>

        <span class="c"># Replace all of the strings in the replacement dictionary</span>
        <span class="k">for</span> <span class="n">match</span><span class="p">,</span> <span class="n">replacement</span> <span class="ow">in</span> <span class="n">replacements</span><span class="o">.</span><span class="n">iteritems</span><span class="p">():</span>
            <span class="n">value</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">match</span><span class="p">,</span> <span class="n">replacement</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">value</span>

    <span class="k">def</span> <span class="nf">__findPage</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Find a page with the given path.</span>

<span class="sd">        * path -- The path for the page</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">pages</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__findErrorPage</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">errorCode</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Find an error page with the given error code.</span>

<span class="sd">        * errorCode -- The error code for the page</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">errorPages</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">errorCode</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__sendResponse</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">page</span><span class="p">,</span> <span class="n">content</span><span class="p">,</span> <span class="n">statusCode</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Send the response for the current request.</span>

<span class="sd">        * page -- The page to return</span>
<span class="sd">        * content -- The page content to return</span>
<span class="sd">        * statusCode -- The status code to return</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="n">statusCode</span> <span class="o">=</span> <span class="n">StatusCodes</span><span class="o">.</span><span class="n">OK</span> <span class="k">if</span> <span class="n">statusCode</span> <span class="ow">is</span> <span class="bp">None</span> <span class="k">else</span> <span class="n">statusCode</span>

        <span class="c"># Write the status code</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send_response</span><span class="p">(</span><span class="n">statusCode</span><span class="p">)</span>

        <span class="c"># Write all of the page headers</span>
        <span class="n">headers</span> <span class="o">=</span> <span class="p">[</span><span class="n">page</span><span class="o">.</span><span class="n">contentType</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">header</span> <span class="ow">in</span> <span class="p">[</span><span class="n">header</span> <span class="k">for</span> <span class="n">header</span> <span class="ow">in</span> <span class="n">headers</span> <span class="k">if</span> <span class="n">header</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">]:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">send_header</span><span class="p">(</span><span class="n">header</span><span class="o">.</span><span class="n">getId</span><span class="p">(),</span> <span class="n">header</span><span class="o">.</span><span class="n">getValue</span><span class="p">())</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">end_headers</span><span class="p">()</span>

        <span class="c"># Finally write the page data</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">wfile</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">content</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="StoppableRequestHandler"><a class="viewcode-back" href="../../../../web.html#pyamp.web.http.requests.StoppableRequestHandler">[docs]</a><span class="k">class</span> <span class="nc">StoppableRequestHandler</span><span class="p">(</span><span class="n">BaseHTTPRequestHandler</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;The StoppableRequestHandler class provides an implementation of</span>
<span class="sd">    an HTTP request handler that handles the quit message send by a</span>
<span class="sd">    server when it is trying to stop itself.</span>

<span class="sd">    &#39;&#39;&#39;</span>

<div class="viewcode-block" id="StoppableRequestHandler.do_QUIT"><a class="viewcode-back" href="../../../../web.html#pyamp.web.http.requests.StoppableRequestHandler.do_QUIT">[docs]</a>    <span class="k">def</span> <span class="nf">do_QUIT</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Handle a QUIT message.&#39;&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send_response</span><span class="p">(</span><span class="mi">200</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">end_headers</span><span class="p">()</span></div></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../../index.html" >Module code</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, Brett Ponsler.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.1.
    </div>
  </body>
</html>